package algorithm.prim;

import java.util.Arrays;

/**
 * 普利姆算法 解决图最小生成树问题 还有克鲁斯卡尔算法
 *
 * @author jack.wu
 * @version 1.0
 * @date 2020-04-07
 */
public class PrimAlgorithm {
    public static void main(String[] args) {
        char[] data = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        int verxs = data.length;
        // 10000 表示不连通
        int[][] weight = {{10000, 5, 7, 10000, 10000, 10000, 2},
                {5, 10000, 10000, 9, 10000, 10000, 3},
                {7, 10000, 10000, 10000, 8, 10000, 10000},
                {10000, 9, 10000, 10000, 10000, 4, 10000},
                {10000, 10000, 8, 10000, 10000, 5, 4},
                {10000, 10000, 10000, 4, 5, 10000, 6},
                {2, 3, 10000, 10000, 4, 6, 10000}};

        MGraph graph = new MGraph(7);
        MinTree minTree = new MinTree();
        minTree.createGraph(graph, verxs, data, weight);
        System.out.println("显示图~~~~~");
        minTree.showGraph(graph);
        System.out.println("Prim 算法~~~~");
        minTree.prim(graph,0);
    }


}

class MinTree {

    /**
     * 创建图
     *
     * @param graph  图对象
     * @param verxs  顶点数
     * @param data   顶点数据
     * @param weight 边
     */
    public void createGraph(MGraph graph, int verxs, char[] data, int[][] weight) {
        int i, j;
        for (i = 0; i < verxs; i++) {
            // 顶点数据
            graph.data[i] = data[i];
            // 边
            for (j = 0; j < verxs; j++) {
                graph.weight[i][j] = weight[i][j];
            }
        }
    }

    /**
     * 显示边 邻接矩阵
     *
     * @param graph 图
     */
    public void showGraph(MGraph graph) {
        for (int[] link : graph.weight) {
            System.out.println(Arrays.toString(link));
        }
    }

    /**
     * prim算法 得到最小生成树
     *
     * @param graph 图
     * @param v     表示从图的第几个顶点开始生成
     */
    public void prim(MGraph graph, int v) {
        // 标记是否被访问 0：未访问 1：已访问
        int[] visited = new int[graph.verxs];
        // 标记当前结点被访问
        visited[v] = 1;
        // 记录两个顶点的下标
        int h1 = -1;
        int h2 = -1;
        int minWeight = 10000;
        // 根据普利姆算法 会得到 顶点和 - 1 条边 所以这里从1开始
        for (int k = 1; k < graph.verxs; k++) {
            for (int i = 0; i < graph.verxs; i++) {
                for (int j = 0; j < graph.verxs; j++) {
                    if (visited[i] == 1 && visited[j] == 0 && graph.weight[i][j] < minWeight) {
                        minWeight = graph.weight[i][j];
                        h1 = i;
                        h2 = j;
                    }
                }
            }
            System.out.println("边<" + graph.data[h1] + "," + graph.data[h2] + "> 权值：" + minWeight);
            // 找到权值最小点 然后标记访问
            visited[h2] = 1;
            minWeight = 10000;
        }

    }

}

class MGraph {
    // 图顶点数
    int verxs;
    // 存放顶点数据
    char[] data;
    // 存放边 使用邻接矩阵
    int[][] weight;

    public MGraph(int verxs) {
        this.verxs = verxs;
        data = new char[verxs];
        weight = new int[verxs][verxs];
    }

}